var inputField;
var timeString = "";
var dateFormat = "YYYY-mm-DD";

var gYear = -1;
var gMonth = -1;

function createCalendarAddFormatDiv(field, format) {
	if (format)
		dateFormat = format;
	initCalendar(field);
}
function createCalendarAddTimeAddFormatDiv(field, ts, format) {
	if (format)
		dateFormat = format;
	if (ts)
		timeString = " " + ts;
	initCalendar(field);
}
function createCalendarAddTimeDiv(field, ts) {
	if (ts)
		timeString = " " + ts;
	initCalendar(field);
}
function createCalendarDiv(field) {
	initCalendar(field);
}

function initCalendar(field) {
	if (removeCalendarDiv())
		return;

	inputField = field;

	inputField.onblur = function(inEvent) {
		removeCalendarDiv();
	};

	var dateString = inputField.value;
	if (dateString == null || dateString.length < dateFormat.length)
		dateString = getCurrentDateString();

	// 존재하는지 체크 가능하면 체크하자.
	var calendarDiv;

	calendarDiv = document.createElement("div");
	calendarDiv.style.zIndex = "200";
	calendarDiv.style.position = "absolute";
	calendarDiv.style.cursor = "hand";

	var posArray = getPosition();
	calendarDiv.style.left = posArray[0] + "px";
	calendarDiv.style.top = posArray[1] + "px";
	calendarDiv.style.opacity = "0.8";
	calendarDiv.style.repeat = "repeat";
	// calendarDiv.style.filter = "alpha(opacity:80)";
	// calendarDiv.style.border = "1px solid #AAAAAA";
	calendarDiv.classname = "calendarActiveScript";
	calendarDiv.id = "calendarActiveScript";
	// calendarDiv.style.visibility = "hidden";
	calendarDiv.style.width = "180";
	calendarDiv.style.height = "145";
	calendarDiv.style.backgroundColor = "white";
	calendarDiv.style.autocomplete = "off";

	calendarDiv.innerHTML = getCalendarDivHtml(dateString);

	calendarDiv.onmousedown = function(inEvent) {
		inEvent = inEvent || window.event;
		var oTarget = inEvent.target || inEvent.srcElement;
		selectDate(oTarget.firstChild.nodeValue);
	};

	document.body.appendChild(calendarDiv);
}

function selectDate(date) {
	var dateS = getDateStringByFormat(gYear, gMonth, date);
	if (date == "<<") {
		setCalendarYear(dateS, -1);
	} else if (date == ">>") {
		setCalendarYear(dateS, 1);
	} else if (date == "<") {
		setCalendarMonth(dateS, -1);
	} else if (date == ">") {
		setCalendarMonth(dateS, 1);
	} else if (!isNaN(date)) {
		if (Number(date) > 0 && Number(date) <= 31) {
			setDataSelect(dateS);
		}
	}
}
function getPosition() {
	var x = inputField.offsetLeft;
	var y = inputField.offsetTop + 20;
	var obj = inputField.offsetParent;

	while (obj.offsetParent) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}

	var temp = [ x, y ];
	return temp;
}
function getCalendarDivHtml(dateString) {
	var dateArray = getDateArray(dateString);
	var html;

	gYear = Number(dateArray[0]);
	gMonth = Number(dateArray[1]);

	html = "<table align='center' width='175' border='0' cellspacing='1' bgcolor='#DDDDDD' cellpadding='0' style='font-size:11px; color:gray;'>";
	html += "<tr width='175' align='center' bgcolor='#A6CAF0' valign='middle'>";
	html += " <td width='25' onmouseover=\"this.style.backgroundColor='#DDDDDD'\" onmouseout=\"this.style.backgroundColor='#A6CAF0'\"><<</td>";
	html += " <td width='25' onmouseover=\"this.style.backgroundColor='#DDDDDD'\" onmouseout=\"this.style.backgroundColor='#A6CAF0'\"><</td>";
	html += " <td colspan=3 style='font-size:13px; color=#ffffff'>"
			+ dateArray[0] + " - " + dateArray[1] + "</td>";
	html += " <td width='25' onmouseover=\"this.style.backgroundColor='#DDDDDD'\" onmouseout=\"this.style.backgroundColor='#A6CAF0'\">></td>";
	html += " <td width='25' onmouseover=\"this.style.backgroundColor='#DDDDDD'\" onmouseout=\"this.style.backgroundColor='#A6CAF0'\">>></td>";
	html += "</tr>";

	html += "<tr width='175' align='center' bgcolor='#FFD9B3' valign='middle'>";
	html += " <td width='25'><font color='#ff6666'>S</font></td>";
	html += " <td width='25'>M</td>";
	html += " <td width='25'>T</td>";
	html += " <td width='25'>W</td>";
	html += " <td width='25'>T</td>";
	html += " <td width='25'>F</td>";
	html += " <td width='25'><font color='#6666ff'>S</font></td>";
	html += "</tr>";

	var firstdate = new Date(dateArray[0], dateArray[1] - 1, 1);// 해당년월의 첫날 구함
	var firstday = firstdate.getDay(); // 해당일의 요일 구함
	var lastDate = getLastDate(dateArray[0], dateArray[1]);

	var writeDate = 1 - firstday;

	for ( var i = 0; i < 6; i++) {
		html += "<tr width='175' align='center' bgcolor='#FFFFFF' valign='middle' align='center'>";
		for ( var j = 0; j < 7; j++) {
			html += "<td width='25' ";
			if (writeDate >= 1 && writeDate <= lastDate)
				html += "onmouseover=\"this.style.backgroundColor='#DDDDDD'\" onmouseout=\"this.style.backgroundColor='#FFFFFF'\">";
			else
				html += ">";

			if (j == 0)
				html += "<font color='#ff6666'>";
			else if (j == 6)
				html += "<font color='#6666ff'>";

			if (writeDate < 1 || writeDate > lastDate)
				html += "&nbsp;";
			else
				html += "" + writeDate;

			if (j == 0 || j == 6)
				html += "</font>";

			html += "</td>";
			writeDate++;
		}
		html += "</tr>";
		if (writeDate > lastDate)
			break;
	}

	html += "</table>";

	return html;
}

function getCurrentDateString() {
	var now = new Date();

	return getDateStringByFormat(now.getFullYear(), now.getMonth() + 1, now
			.getDate());
}

function getDateStringByFormat(y, m, d) {
	y = Number(y);
	m = Number(m);
	d = Number(d);
	var yString = y.toString();
	var mString = (m < 10 ? "0" : "") + m.toString();
	var dString = (d < 10 ? "0" : "") + d.toString();

	var dateStringResult = dateFormat;
	dateStringResult = dateStringResult.replace(/yyyy/gi, yString);
	dateStringResult = dateStringResult.replace(/mm/gi, mString);
	dateStringResult = dateStringResult.replace(/dd/gi, dString);

	return dateStringResult;
}

function getDateArray(s) {
	var a = new Array(3);
	var yIndex = dateFormat.toLowerCase().indexOf("yyyy");
	var mIndex = dateFormat.toLowerCase().indexOf("mm");
	var dIndex = dateFormat.toLowerCase().indexOf("dd");

	a[0] = s.substring(yIndex, yIndex + "yyyy".length);
	a[1] = s.substring(mIndex, mIndex + "mm".length);
	a[2] = s.substring(dIndex, dIndex + "dd".length);

	return a;
}

function getLastDate(year, month) {
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
			|| month == 10 || month == 12)
		return 31;
	else if (month == 4 || month == 6 || month == 9 || month == 11)
		return 30;

	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
		return 29;

	return 28;
}

function setDataSelect(dateString) {
	var dateArray = getDateArray(dateString);
	var dateStringResult = getDateStringByFormat(dateArray[0], dateArray[1],	dateArray[2]);
	dateStringResult += timeString;

	inputField.value = dateStringResult;
	removeCalendarDiv();
}

function setCalendarYear(dateString, changeY) {
	var dateArray = getDateArray(dateString);
	dateArray[0] = Number(dateArray[0]) + changeY;
	document.getElementById("calendarActiveScript").innerHTML = getCalendarDivHtml(getDateStringByFormat(
			dateArray[0], dateArray[1], dateArray[2]));
}
function setCalendarMonth(dateString, changeM) {
	var dateArray = getDateArray(dateString);
	dateArray[1] = Number(dateArray[1]) + changeM;
	if (dateArray[1] < 1) {
		dateArray[0] = Number(dateArray[0]) - 1;
		dateArray[1] = 12 + Number(dateArray[1]);
	} else if (dateArray[1] > 12) {
		dateArray[0] = Number(dateArray[0]) + 1;
		dateArray[1] = Number(dateArray[1]) - 12;
	}

	document.getElementById("calendarActiveScript").innerHTML = getCalendarDivHtml(getDateStringByFormat(
			dateArray[0], dateArray[1], dateArray[2]));
}

function removeCalendarDiv() {
	if (document.getElementById("calendarActiveScript") != null) {
		var e = document.getElementById("calendarActiveScript");
		document.body.removeChild(e);

		return true;
	}

	return false;
}